package com.grieous.wow.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grieous.wow.biz.FamilyBiz;
import com.grieous.wow.common.*;
import com.grieous.wow.entity.PageModel;
import com.grieous.wow.entity.Family;
import com.grieous.wow.entity.User;

@Controller
@RequestMapping("/family")
public class FamilyController {

	@Resource
	private FamilyBiz familyBiz;

	@ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	@RequestMapping("/manage")
	public String manage(HttpServletRequest request, Integer page) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			PageModel pm = familyBiz.getFamily(Page.HALF_DEFAULT_PAGESIZE,
					page, null, null);
			request.setAttribute("pageModel", pm);
			return "admin/family/family";
		}
		return null;
	}

	@RequestMapping("/add")
	public String add(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			return "admin/family/saveFamily";
		}
		return null;
	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			Family family = familyBiz.getFamilyById(id);
			if (family != null) {
				request.setAttribute("family", family);
			}

			return "admin/family/saveFamily";
		}
		return null;
	}

	@RequestMapping("/save")
	public String save(HttpServletRequest request,
			HttpServletResponse response, Family family) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			if (familyBiz.saveFamily(family))
				out.print("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "admin/family/family";
	}

	@RequestMapping("/delete")
	public String delete(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if (familyBiz.delFamily(ids))
					out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/vote", produces = "text/html;charset=UTF-8")
	public @ResponseBody
	String vote(Integer fid, HttpServletRequest request) {
		String res = familyBiz.voteFamily(fid, Common.getIP(request));
		return new String(res);
	}

	@RequestMapping("/post")
	public String post() {

		return "post/postFamily";
	}

	@RequestMapping(value = "/post/save", method = RequestMethod.POST)
	public @ResponseBody
	String postSave(Family family) {
		if (familyBiz.saveFamily(family))
			return new String("success");
		return new String("fail");
	}

	@RequestMapping(value = "/check")
	public @ResponseBody
	Boolean checkFamilyName(String name) {
		boolean result = familyBiz.checkFamilyName(name);
		return result;
	}
}
